package com.qcs.sleep.warn;

import com.qcs.sleep.OnWarnListener;
import com.qcs.sleep.log.Log4J;

/**
 * Created by chshqiang on 13-10-17.
 * 上网冲浪示警
 */
public class WebWarnAdapter extends LauncherWarnAdapter {
    // com.qzone
    // com.UCMobile
    // com.android.browser
    private final int FIRST_GOON_LINE_DELAY = 45 * 60;

    private OnWarnListener mListener;

    private boolean isGoonLine = false;

    private int nextWarnDelay = FIRST_GOON_LINE_DELAY;
    private int goonLineTime = 0;

    @Override
    public void run() {
        if (isGoonLine) {
            goonLineTime++;

            if (goonLineTime >= nextWarnDelay) {
                nextWarnDelay += nextWarnDelay / 6;

                mListener.onWarn(59, OnWarnListener.FLAG_ACTIVITY, null);
            }
        } else {
            if (0 >= goonLineTime)
                return;

            goonLineTime -= goonLineTime / 10; // 有过上网行为，此时未上网，则削弱上网的总计时
        }

//        Log4J.m("isGoonLine: " + isGoonLine + ", goonLineTime: " + goonLineTime);
    }

    @Override
    public void onReceive(int flag, String action) {
        if (APP != flag) {
            return;
        }

        isGoonLine = (action.contains("com.qzone") || action.contains("com.UCMobile") || action.contains("com.android.browser"));
    }

    @Override
    public void setOnWarnListener(OnWarnListener listener) {
        mListener = listener;
    }
}
